package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbNewTechnologyParam;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbNewTechnologyParamDao {

    @Results(id = "pcbNewTechnologyParamResultMap", value = {
            @Result(column = "param_id", property = "paramId", jdbcType = JdbcType.INTEGER),
            @Result(column = "param_name", property = "paramName", jdbcType = JdbcType.INTEGER),
            @Result(column = "param_name_en", property = "paramNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "param_key", property = "paramKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "param_type", property = "paramType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "param_options", property = "paramOptions", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "unit", property = "unit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_required", property = "isRequired", jdbcType = JdbcType.INTEGER),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })
    /*
     * 单个查询
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM +
            " where 1 = 1 " +
            "<if test='id != null'>" +
            " and param_id = #{id}" +
            "</if>" +
            " order by sort" +
            "</script>")
    PcbNewTechnologyParam getAllById(@Param("id") Integer id);

    /*
     * 单个查询
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM +
            " where 1 = 1 " +
            "<if test='paramKey != null'>" +
            " and param_key = #{paramKey}" +
            "</if>" +
            " order by sort" +
            "</script>")
//    @ResultMap(value = "pcbNewTechnologyParamResultMap")
    PcbNewTechnologyParam getByParamKey(@Param("paramKey") String paramKey);

    /*
     * 查询指定行数据
     */
    @Select("<script> " +
            "select param_id, param_name, param_name_en, param_key, param_type, param_options, is_required, remark, create_time, create_user, update_time, update_user, status, sort " +
            " from " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM +
            "<where>" +
            "<if test='paramId != null'>" +
            " and param_id = #{paramId}" +
            "</if>" +
            "<if test='paramName != null '>" +
            " and param_name = #{paramName}" +
            "</if>" +
            "<if test='paramNameEn != null '>" +
            " and param_name_en = #{paramNameEn}" +
            "</if>" +
            "<if test='paramKey != null '>" +
            " and param_key = #{paramKey}" +
            "</if>" +
            "<if test='paramType != null '>" +
            " and  param_type = #{paramType}" +
            "</if>" +
//            "<if test='unit != null '>" +
//            " and unit = #{unit}" +
//            "</if>" +
            "<if test='remark != null '>" +
            " and remark = #{remark}" +
            "</if>" +
            "<if test='status != null '>" +
            " and status = #{status}" +
            "</if>" +
            "<if test='sort != null '>" +
            " and sort = #{sort}" +
            "</if>" +
            "</where>" +
            "order by sort" +
            "</script>")
    @ResultMap(value = "pcbNewTechnologyParamResultMap")
    List<PcbNewTechnologyParam> getAllByLimit(PcbNewTechnologyParam pcbMaterial);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM + " (param_name, param_name_en, param_key, param_type, param_options,  is_required, remark, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{paramName}, #{paramNameEn}, #{paramKey}, #{paramType}, #{paramOptions},  #{isRequired}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbMaterialParamId")
    Integer insert(PcbNewTechnologyParam param);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM + " where param_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_NEW_TECHNOLOGY_PARAM +
            "<set> " +
            "<if test='paramName != null'>" +
            "param_name = #{paramName}," +
            "</if>" +
            "<if test='paramNameEn != null'>" +
            "param_name_en = #{paramNameEn}," +
            "</if>" +
            "<if test='isRequired != null '>" +
            "is_required = #{isRequired}," +
            "</if>" +
            "<if test='paramType != null '>" +
            "param_type = #{paramType}," +
            "</if>" +
            "<if test='paramOptions != null and paramOptions != \"\"'>" +
            "param_options = #{paramOptions}," +
            "</if>" +
//            "<if test='unit != null and unit != \"\"'>" +
//            "unit = #{paramOptions}," +
//            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "create_user = #{createUser}," +
            "</if>" +
            "<if test='createTime != null '>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "update_user =#{updateUser}," +
            "</if>" +
            "<if test='updateTime != null '>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null '>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null '>" +
            "sort = #{sort} " +
            "</if>" +
            "</set> " +
            "where param_id = #{paramId}" +
            "</script>")
    Integer update(PcbNewTechnologyParam param);
}
